package com.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.mapper.AddressMapper;
import com.shop.mapper.OrdersMapper;
import com.shop.mapper.UserMapper;
import com.shop.pojo.Address;
import com.shop.pojo.Orders;
import com.shop.pojo.User;
import com.shop.service.OrdersService;
import com.shop.utils.CodeUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Transactional
public class OrdersServiceImpl implements OrdersService {
    @Autowired
    private OrdersMapper orderMapper;
    @Autowired
    private AddressMapper addressMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public Page<Orders> pageQuery(Integer pagenum, Integer pageSize, String pageQuery) {
        QueryWrapper<Orders> wrapper = new QueryWrapper<>();
        Page<Orders> page = new Page<>(pagenum,pageSize);
        if (pageQuery!=null&&pageQuery.length()>0){
            wrapper.like("orders_id",pageQuery);
            page = orderMapper.selectPage(page,wrapper);
            List<Orders> userList = page.getRecords();
            List<Orders> addressList = page.getRecords();
            for (Orders order:
                    userList) {
                String userName = orderMapper.findUserNameByAddressId(order.getId());
                order.setUserName(userName);
            }
            for (Orders order:
                    addressList) {
                String addressName = orderMapper.findAddressNameByUserId(order.getId());
                order.setAddressName(addressName);
            }
            page.setRecords(userList);
            page.setRecords(addressList);
        }else{
            page = orderMapper.selectPage(page,null);
            List<Orders> userList = page.getRecords();
            List<Orders> addressList = page.getRecords();
            for (Orders order:
                    userList) {
                String userName = orderMapper.findUserNameByAddressId(order.getId());
                order.setUserName(userName);
            }
            for (Orders order:
                    addressList) {
                String addressName = orderMapper.findAddressNameByUserId(order.getId());
                order.setAddressName(addressName);
            }
            page.setRecords(userList);
            page.setRecords(addressList);
        }
        return page;
    }

    @Override
    public void edit(Orders orders) {
        orders.setStatus(1);
        orderMapper.updateById(orders);
    }

    @Override
    public Orders add(Orders orders) {
        orders.setCode(CodeUtils.getCode());
        orders.setStatus(0);
        orders.setTime(LocalDateTime.now());
        orderMapper.insert(orders);
        return orders;
    }

    @Override
    public Page<Orders> getPage(Integer uid, Integer pagenum, Integer pagesize) {
        QueryWrapper<Orders> wrapper = new QueryWrapper<>();
        Page<Orders> page = new Page<>(pagenum,pagesize);
        wrapper.eq("user_id",uid);
        page = orderMapper.selectPage(page,wrapper);
        List<Orders> records = page.getRecords();
        for (Orders o:
             records) {
            int aid = o.getAddressId();
            Address address = addressMapper.selectById(aid);
            o.setAddressName(address.getName());
            int userId = o.getUserId();
            User user = userMapper.selectById(userId);
            o.setUserName(user.getUsername());
        }
        page.setRecords(records);
        return page;
    }
}
